package com.skyz.base.util;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.skyz.base.R;
import com.skyz.base.activity.BaseActivity;

public class AppUtils {
    private AppUtils() {
    }


    public static String getAppVersion(Context context) {
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }


    public static int getAppVersionCode(Context context) {
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }


    public static void showToast(BaseActivity context, String msg) {
        View view = context.getLayoutInflater().inflate(R.layout.toast_layout, null);
        TextView text = (TextView) view.findViewById(R.id.textView);
        text.setText(msg);
        Toast toast = new Toast(context.getApplicationContext());
        toast.setView(view);
        toast.show();
    }

    public static String getHtml5BodyStr (String bodyHtml) {
        String style = "<style>\n" +
                "    body,ul,ol,li,p,h1,h2,h3,h4,h5,h6,form,fieldset,table,td,img,div{margin:0;padding:0;border:0;}\n" +
                "   ul,ol{list-style:none;}\n" +
                "   select,input,img,select{vertical-align:middle;}\n" +
                "   a{text-decoration:none;}\n" +
                "    img{\n" +
                "        max-width: 100% !important;\n" +
                "        height: auto !important;\n" +
                "    }\n" +
                "    video{\n" +
                "        max-width: 100% !important;\n" +
                "        height: auto !important;\n" +
                "    }\n" +
                "</style>";
        String js = "<script>\n" +
                "var imgs=document.getElementsByTagName('img');\n" +
                "for(var i=0;i<imgs.length;i++){\n" +
                "imgs[i].onclick=function(){\n" +
                "wv.onImageClick('click', this.src);\n" +
                "};\n" +
                "}\n" +
                "</script>";

        String html = "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "<meta charset=\"utf-8\">\n" +
                "<title></title>\n" +
                style +
                "</style>\n" +
                "</head>\n" +
                "<body>\n" +
                bodyHtml +
                js +
                "\n" +
                "</body>\n" +
                "</html>";

        return html;
    }


    public static String getAndroidId(Context context) {
        String deviceId = "";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (context.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
                TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    deviceId = telephonyManager.getImei();
                }
            }
        } else {
            deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        }
        if (deviceId == null) {
            deviceId = "";
        }
        return deviceId;
    }


    public static int compareVersion(String onLineVersion, String appVersion) {
        String[] arr1 = onLineVersion.split("\\."); // 使用 . 分割版本号字符串
        String[] arr2 = appVersion.split("\\.");

        int i = 0;
        while (i < arr1.length && i < arr2.length) {
            int v1 = Integer.parseInt(arr1[i]);
            int v2 = Integer.parseInt(arr2[i]);
            if (v1 != v2) {
                return v1 > v2 ? 1 : -1;
            }
            i++;
        }
        while (i < arr1.length) {
            if (Integer.parseInt(arr1[i]) != 0) {
                return 1;
            }
            i++;
        }
        while (i < arr2.length) {
            if (Integer.parseInt(arr2[i]) != 0) {
                return -1;
            }
            i++;
        }
        return 0;
    }

}
